package mygang.model.constructor;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.*;
import mygang.model.BaseModel;
import mygang.model.Option;
import mygang.model.OptionInterface;
import org.hibernate.annotations.Proxy;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 22/09/2012
 */
@Entity()
@Table(name = "FORM")
@Proxy(lazy = false)
public class Form extends BaseModel implements Serializable, TitleInterface, OptionInterface {

    @Id
    @Column(name = "FORM_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    @JoinTable(name = "FORM_STEP_MAP",
    joinColumns = {@JoinColumn(name = "FORM_ID")},
    inverseJoinColumns = {@JoinColumn(name = "STEP_ID")})
    private Set<Step> steps;
    
    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="FT_ID")
    private FormType type;
    
    @Column(name = "FORM_NAME")
    private String title;
    
    @Column(name = "FORM_DESCRIPTION")
    private String description;
    
    public Form() {
    }
    
    public Form(long id) {
        this.id = id;
    }

    public Form(long id, FormType type, String title, String description) {
        this(type, title, description);
        this.id = id;
    }

    public Form(FormType type, String title, String description) {
        this.type = type;
        this.title = title;
        this.description = description;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Set<Step> getSteps() {
        return steps;
    }

    public void setSteps(Set<Step> steps) {
        this.steps = steps;
    }

    public String getTypeName() {
        return type.getName();
    }
    
    public FormType getType() {
        return type;
    }

    public void setType(String type) {
        this.type.setName(type);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
         this.description = description;
    }

    public Option toOption() {
        return new Option(getId(), getTitle());
    }
}